package com.mall507.fivepeaks.activity.find;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Switch;
import android.widget.TextView;

import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hzh.frame.core.ImageFrame.BaseImage;
import com.hzh.frame.ui.fragment.AbsRecyclerViewFM;
import com.hzh.frame.util.StatusBarUtil;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.hzh.frame.widget.xwebview.XWebViewActivity;
import com.mall507.comn.model.Find;
import com.mall507.comn.model.FindItem;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.Util;
import com.mall507.fivepeaks.widget.xgridview.XGridView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version 1.0
 * @date 2019/7/30
 */
public class FindRFM extends AbsRecyclerViewFM<Find> {

    @Override
    protected int setLayoutId() {
        return R.layout.fm_rv_find_index;
    }

    @Override
    protected void bindView(View view) {
        getTitleView().addView(StatusBarUtil.createStatusBarView(getActivity(), ContextCompat.getColor(getActivity(),R.color.application_color)),0);
        getTitleView().setLeftIsShow(false);
        getTitleView().setContent("发现");
    }

    @Override
    protected String setHttpPath() {
        return "Explorer/getExplorer";
    }

    @Override
    protected List<Find> handleHttpData(JSONObject response) {
        List<Find> list = new ArrayList<>();
        if(200==response.optInt("code")){
            JSONArray jsonArray=response.optJSONArray("data");
            for(int i=0;i<jsonArray.length();i++){
                JSONObject object=jsonArray.optJSONObject(i);
                Find model=new Find();
                model.setNid(object.optString("id"));
                model.setName(object.optString("name"));
                JSONArray itemJsonArray=object.optJSONArray("item");
                new Delete().from(FindItem.class).where("pid = ?",model.getNid()).execute();
                for (int j=0;j<itemJsonArray.length();j++){
                    JSONObject itemObject=itemJsonArray.optJSONObject(j);
                    FindItem item = new FindItem();
                    item.setPid(model.getNid());
                    item.setNid(itemObject.optString("id"));
                    item.setName(itemObject.optString("name"));
                    item.setClickType(itemObject.optString("clickType"));
                    item.setClickValue(itemObject.optString("clickValue"));
                    item.save();
                }
                list.add(model);
            }
        }
        return list;
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_find_index;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, Find model) {
        holder.setText(R.id.typeName,"· "+model.getName());
        
        XGridView gridView= holder.getView(R.id.gview);
        List<FindItem> list = new Select().from(FindItem.class).where("pid = ?",model.getNid()).execute();
        if(list !=null && list.size()>0){
            gridView.setVisibility(View.VISIBLE);
            gridView.setAdapter(new GridViewAdapter(getContext(), model2HashMap(list), R.layout.item_gv_find, new String[]{"content"}, new int[]{R.id.content}));
        }else{
            gridView.setVisibility(View.GONE);
        }
    }
    
    public List<HashMap<String, String>> model2HashMap(List<FindItem> listModel){
        List<HashMap<String, String>> listMap = new ArrayList<>();
        for (FindItem model: listModel){
            HashMap<String,String> map = new HashMap<>();
            map.put("clickType",model.getClickType());
            map.put("clickValue",model.getClickValue());
            map.put("content",model.getName());
            map.put("id",model.getNid());
            listMap.add(map);
        }
        return listMap;
    }

    private class GridViewAdapter extends SimpleAdapter {
        private List<HashMap<String, String>> resourceList;

        public GridViewAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
            resourceList=(List<HashMap<String, String>>) data;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view=super.getView(position, convertView, parent);
            final HashMap<String,String> map = resourceList.get(position);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (map.get("clickType")){
                        case "1"://遗失填写
                            ARouter.getInstance().build("/find/FindLostWriteUI").navigation();
                            break;
                        case "2"://失物招领
                            ARouter.getInstance().build("/find/FindLostFoundUI").navigation();
                            break;
                        case "3": //投稿发布
                            ARouter.getInstance().build("/find/FindSubmitReleaseUI").navigation();
                            break;
                        case "4": //平台合作
                            ARouter.getInstance().build("/find/FindCooperationUI").navigation();
                            break;
                        case "5":
                            if (map.get("id").equals("5")){
                                ARouter.getInstance().build(map.get("clickValue")).navigation();
                            }else if (map.get("id").equals("2")){
                                ARouter.getInstance().build(map.get("clickValue")).navigation();
                            }else if (map.get("id").equals("4")){
                                ARouter.getInstance().build(map.get("clickValue")).navigation();
                            }else {
                                Intent in3 = new Intent(getActivity(), XWebViewActivity.class);
                                in3.putExtra("url", map.get("clickValue"));
                                getActivity().startActivity(in3);
                            }
                            break;
                    }
                }
            });
            return view;
        }
    }
}
